<?php

class DB {

    private static $connection = null;
    protected $db_connection = null;
    public $lists = array(), $currencies, $messages;
/* ------------------------------------------------------------------------------------------------- */


    public function __construct() {
        if(self::$connection == null) {
            $config = Config::get_instance();
            self::$connection = new mysqli(
                $config->get_config('db_host'),
                $config->get_config('db_user'),
                $config->get_config('db_pass'),
                $config->get_config('db_database'),
                $config->get_config('db_port')
            );

            if(self::$connection->connect_error) {
                die('Connection error');
            }
            self::$connection->set_charset("utf8");
        }

        //that will be used in code
        $this->db_connection = self::$connection;
    }
/* ------------------------------------------------------------------------------------------------- */


    //dynamically create class properties
    protected function create_property($var_name, $val){
        $this->lists[$var_name][] = $val;
    }
/* ------------------------------------------------------------------------------------------------- */


    public function get_states() {
        $query = $this->db_connection->query('SELECT name FROM lists_states WHERE is_active=1');
        while ($row = $query->fetch_assoc()) {
            $this->lists['states'][] = $row['name'];
        }

        return $this;
    }
/* ------------------------------------------------------------------------------------------------- */


    public function get_currencies() {
        $query = $this->db_connection->query("SELECT * FROM lists_currencies WHERE is_active=1");

        while($row = $query->fetch_assoc()) {
            $this->currencies[$row['currency']] = $row['rate_to_naira'];
        }

        return $this;
    }
/* ------------------------------------------------------------------------------------------------- */


    public function get_currency_rate($curr) {
        $query = $this->db_connection->query("SELECT rate_to_naira FROM lists_currencies
            WHERE currency='$curr' AND is_active=1");

        if($query->num_rows == 1) {
            $row = $query->fetch_assoc();
            $this->currencies['selected_currency'] = $curr;
            return $row['rate_to_naira'];
        } else { //default currency - Naira
            return 1;
        }
    }
/* ------------------------------------------------------------------------------------------------- */


    public function update_currencies($data) {
        foreach ($data as $v) {
            $clean_data[] = htmlspecialchars(trim($v));
        }

        $r = '';
        for($j=1, $i=0, $k=1; $j < 6; $j++, $i=$i+2, $k=$k+2) {
            $query = $this->db_connection->query("UPDATE lists_currencies SET
              currency='$clean_data[$i]',rate_to_naira='$clean_data[$k]' WHERE curr_id=({$j}+1)");
            if($query) {
                $r .= 'y';
            }
        }

        if($r !== 'yyyyy') {
            $this->messages[] = 'There has been a problem with your request. Please check the database table lists_currencies manually.';
        } else {
            $this->messages['success'] = true;
        }

        return $this;
    }
    /* ------------------------------------------------------------------------------------------------- */

}